package com.xyz.CollectionSortTest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Student> ss = new ArrayList<>();
        ss.add(new Student("101", "小红", "女", 15));
        ss.add(new Student("002", "小黄", "男", 18));
        ss.add(new Student("503", "小黑", "女", 12));
        ss.add(new Student("004", "小白", "女", 25));
        //Collections.sort(ss);
        //在排序的同时，使用 Comparator 接口
        Collections.sort(ss,
                new Comparator<Student>(){
                    //按学生年龄比较
                    @Override
                    public int compare(Student o1, Student o2) {
                        if(o1.getAge()>o2.getAge())
                        {
                            return 1;
                        }
                        else if(o1.getAge()==o2.getAge())
                        {
                            return 0;
                        }
                        else {
                            return  -1;
                        }
                    }
                });
        for(Student s : ss){
            System.out.println(s.toString());
        }
    }
}

//基础学生类
class Student
{
    private String id;
    private String name;
    private String gender;
    private int age;

    public Student(String id, String name, String gender, int age) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    //如果不写任何构造器，系统会自动添加空构造器
    //如果谢了任意非空构造器，系统就不再添加空的
    //这是如果需要空构造器，需要手动添加
    public Student() {
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getGender() {
        return gender;
    }

    public int getAge() {
        return age;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age='" + age + '\'' +
                '}';
    }

    /*
    // 按照年龄排序
    @Override
    public int compareTo(Object o) {
        Student s = (Student)o;
        if(this.getAge()<s.getAge())
        {
            return -1;
        }
        else if(this.getAge()==s.getAge()){
            return 0;
        }
        else {
            return 1;
        }
    }*/

    /*
    //按照 id 排序
    @Override
    public int compareTo(Object o) {
        //将学生的 id 放入数组列表
        ArrayList<String> ss = new ArrayList<>();
        ss.add(this.getId());
        Student s = (Student)o;
        ss.add(s.getId());

        //因为String 不能直接用 > < 比较
        //所以现将其放入集合排序
        //根据排序结果，来判断 String 大小
        Collections.sort(ss);

        //如果排序后，字符串在前，说明他较小
        //对比后，小于，返回 -1
        if(ss.get(0).equals(this.getId()))
        {
            return -1;
        }
        else {
            return 1;
        }
        //学生 id 不能重复，所以不存在相等，就不需要返回0
    }
    */

}
